package ict;

import java.io.*;
import java.net.*;
import java.util.ArrayList;

import ict.mobile.R;
import android.app.Activity;
import android.os.Bundle;

import android.content.*;
import android.database.*;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;
import android.graphics.*;
import android.view.*;
import android.widget.*;

public class barChartActivity extends Activity {
	SQLiteDatabase db;
	String sql;
	Cursor cursor = null;

	ImageView barChartImageV;
	String urlPic = "http://chart.apis.google.com/chart?chxr=0,2,200&chxt=y&chbh=a&chs=300x225&cht=bvg&chco=A2C180,3D7930,FF9900,AA0033,2600FF&chds=0,100,0,91.667&chd=t:10|5|6|7|9&chdl=prod01|prod02|prod03|prod04|prod05&chtt=Quantity+in+shop";
	ArrayList l = new ArrayList();
	final static String urlGoogleChart = "http://chart.apis.google.com/chart?chxr=0,2,1000&chxt=y&chbh=a&chs=300x225&cht=bvg&chco=A2C180,3D7930,FF9900,AA0033,2600FF&chds=-5,1000,0,1000,0,1000,0,1000,0,1000&chd=t:";
	// final static String urlp3Api =
	// "10|5|6|7|9&chdl=prod01|prod02|prod03|prod04|prod05&chtt=Quantity+in+shop";

	final static String urlp3Api = "&chdl=prod01|prod02|prod03|prod04|prod05&chtt=Quantity+in+shop";

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.chartdisplay);

		findViewByID();
		selectAllFromProduct();
		barChartImageV.setImageBitmap(getURLBitmap());
	}

	public void findViewByID() {
		barChartImageV = (ImageView) findViewById(R.id.barChartImageV);
	}

	public Bitmap getURLBitmap() {
		URL imageUrl = null;
		Bitmap bitmap = null;
		try {
			String newURL = l.get(0).toString() + "|" + l.get(1).toString()+ "|"
					+ l.get(2).toString() + "|"+ l.get(3).toString()+ "|"
					+ l.get(4).toString() + urlp3Api;

			imageUrl = new URL(urlGoogleChart+newURL);
		} catch (MalformedURLException e) {
			e.printStackTrace();
		}
		try {
			HttpURLConnection conn = (HttpURLConnection) imageUrl
					.openConnection();
			conn.connect();
			InputStream is = conn.getInputStream();
			bitmap = BitmapFactory.decodeStream(is);
			is.close();

		} catch (IOException e) {
			e.printStackTrace();
		}
		return bitmap;
	}

	// ///display Product
	public void selectAllFromProduct() {
		try {
			db = SQLiteDatabase.openDatabase("/data/data/ict.mobile/icsDB",
					null, SQLiteDatabase.OPEN_READONLY);

			cursor = db.rawQuery("select * from Product", null);
			// Toast.makeText(this, "OK", Toast.LENGTH_LONG).show();

			// ProductCode, ProductName, Quantity
			String dataStr = "";
			while (cursor.moveToNext()) {
				int Quantity = cursor.getInt(2);
				dataStr = Quantity+"";
				l.add(dataStr);
			}

			db.close();

		} catch (SQLiteException e) {
			Toast.makeText(this, e.getMessage(), Toast.LENGTH_LONG).show();
		}

	}

	public void genChart() {
		// String url =
		// "http://chart.apis.google.com/chart?chxr=0,2,200&chxt=y&chbh=a&chs=300x225&cht=bvg&chco=A2C180,3D7930,FF9900,AA0033,2600FF&chds=0,100,0,91.667&chd=t:10|5|6|7|9&chdl=prod01|prod02|prod03|prod04|prod05&chtt=Vertical+bar+chart";
		String urlRqs3DPie = urlGoogleChart + urlp3Api;

		Bitmap bm3DPie = loadChart(urlRqs3DPie);
		if (bm3DPie == null) {
			Toast.makeText(this, "Problem in loading 3D Pie Chart",
					Toast.LENGTH_LONG).show();
		} else {
			barChartImageV.setImageBitmap(bm3DPie);
		}

	}

	private Bitmap loadChart(String urlRqs) {
		Bitmap bm = null;
		InputStream inputStream = null;

		try {
			inputStream = OpenHttpConnection(urlRqs);
			bm = BitmapFactory.decodeStream(inputStream);
			inputStream.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return bm;
	}

	private InputStream OpenHttpConnection(String strURL) throws IOException {
		InputStream is = null;
		URL url = new URL(strURL);
		URLConnection urlConnection = url.openConnection();

		try {
			HttpURLConnection httpConn = (HttpURLConnection) urlConnection;
			httpConn.setRequestMethod("GET");
			httpConn.connect();

			if (httpConn.getResponseCode() == HttpURLConnection.HTTP_OK) {
				is = httpConn.getInputStream();
			}
		} catch (Exception ex) {
		}

		return is;
	}
}
